package com.hehan.hhdemo.service.impl;

import com.hehan.hhdemo.controller.UploadController;
import com.hehan.hhdemo.service.IUploadService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.UUID;

/**
 * Created by hehan on 2018/11/16.
 */
@Service
public class UploadServiceImpl implements IUploadService {

    private static final Logger logger = LoggerFactory.getLogger(UploadServiceImpl.class);

    @Override
    public String uploadFile(MultipartFile multipartFile) throws Exception {

        if (multipartFile.isEmpty()) {
            logger.warn("upload file is empty");
            throw new FileNotFoundException("上传文件为空");
        }
        String fileName = multipartFile.getOriginalFilename();
        String preFix=fileName.substring(fileName.lastIndexOf(".")+1);
        Integer ran=(int)(Math.random()*9000)+1000;
        String fileUUIDname = new String(ran.toString());
        String NewFileName = fileUUIDname + "." + preFix;
        String uploadpath =  "E:\\p\\";
        File targetFile = new File(uploadpath, NewFileName);
        if (!targetFile.exists()) {
            targetFile.createNewFile();
        }
        multipartFile.transferTo(targetFile);

        //shangchuan
        targetFile.delete();
        return uploadpath;
    }
}
